package de.jadehs.cosima;

import org.eclipse.xtext.common.services.Ecore2XtextTerminalConverters;
import org.eclipse.xtext.conversion.IValueConverter;
import org.eclipse.xtext.conversion.ValueConverter;
import org.eclipse.xtext.conversion.ValueConverterException;
import org.eclipse.xtext.parsetree.AbstractNode;

public class CosimaValueConverterService extends Ecore2XtextTerminalConverters {

	@ValueConverter(rule = "CHAR")
	public IValueConverter<Character> CHAR() {
		return new IValueConverter<Character>() {
			@Override
			public Character toValue(String string, AbstractNode node)
					throws ValueConverterException {
				// accept characters that match this: 'c'
				return string.length() == 3 ? string.charAt(1) : null;
			}

			@Override
			public String toString(Character value)
					throws ValueConverterException {
				return "'" + value + "'";
			}

		};
	}
	
	@ValueConverter(rule = "EPS")
	public IValueConverter<Character> EPS() {
		return new IValueConverter<Character>() {
			@Override
			public Character toValue(String string, AbstractNode node)
					throws ValueConverterException {
				// accept characters that match this: eps
				return CosimaUtil.EPSILON;
			}

			@Override
			public String toString(Character value)
					throws ValueConverterException {
				return "eps";
			}

		};
	}

	@ValueConverter(rule = "CHAR_WO_TICKS")
	public IValueConverter<Character> CHAR_WO_TICKS() {
		return new IValueConverter<Character>() {
			@Override
			public Character toValue(String string, AbstractNode node)
					throws ValueConverterException {
				// accept characters that match this: c
				return string.length() == 1 ? string.charAt(0) : null;
			}

			@Override
			public String toString(Character value)
					throws ValueConverterException {
				return value.toString();
			}

		};
	}
}
